#include "mainWnd.h"
#include <QtGui/QMessageBox>
#include <QtGui/QTreeWidgetItem>
#include <QDebug>

AppMainWnd::AppMainWnd( QWidget *parent /*= 0*/ )
{
	setupUi(this);
	initWnd();
	_funcParentLst = new QList<QTreeWidgetItem*>();
}

AppMainWnd::~AppMainWnd()
{

}

void AppMainWnd::setNavagationTitle(const QString& title)
{
	this->treeWidgetFunc->setHeaderLabel(title);
}

void AppMainWnd::initWnd()
{
	QHeaderView* header = new QHeaderView(Qt::Horizontal);
	header->setClickable(false);
	header->setMovable(false);
	header->setResizeMode(QHeaderView::Fixed);  
	header->setDefaultSectionSize(200); 
	treeWidgetFunc->setHeader(header);
	
}

void AppMainWnd::showMessage(const QString& title,  const QString& msg )
{
	QMessageBox::information(this, title, msg);
}

int AppMainWnd::addNavagationGroup( const QString& groupName )
{
	QTreeWidgetItem *group = new QTreeWidgetItem(treeWidgetFunc);
	group->setText(0, groupName);
	_funcParentLst->append(group);
	return _funcParentLst->length() - 1;
}

void AppMainWnd::addNavagationFunction( const QString& funcName, int groupId )
{
	QTreeWidgetItem* parent = _funcParentLst->at(groupId);
	QTreeWidgetItem *func = new QTreeWidgetItem(parent);
	func->setText(0, funcName);
}

void AppMainWnd::addNavagationCheck( const QString& funcName, int groupId )
{
	QTreeWidgetItem* parent = _funcParentLst->at(groupId);
	QTreeWidgetItem *func = new QTreeWidgetItem(parent);
	func->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
	func->setText(0, tr("River"));
	func->setCheckState(0, Qt::Unchecked);
	func->setText(0, funcName);
}

void AppMainWnd::onNavagationCheck( QTreeWidgetItem * item, int column )
{
	qDebug()<<"---------------------";
	QString name = item->text(column);
	bool checked = item->checkState(column) == Qt::Checked;
	emit checkChanged(name, checked);
}

void AppMainWnd::onNavagationDoubleClick( QTreeWidgetItem * item, int column )
{
	QString name = item->text(column);
	emit bottonClicked(name);
}

void AppMainWnd::enableNavagation( bool enable )
{
	if(enable)
	{
		connect(treeWidgetFunc, SIGNAL(itemChanged (QTreeWidgetItem*, int)), this, SLOT(onNavagationCheck(QTreeWidgetItem*, int)));
		connect(treeWidgetFunc, SIGNAL(itemDoubleClicked (QTreeWidgetItem*, int)), this, SLOT(onNavagationCheck(QTreeWidgetItem*, int)));
	}
	else
	{
		disconnect(treeWidgetFunc, SIGNAL(itemChanged (QTreeWidgetItem*, int)), this, SLOT(onNavagationCheck(QTreeWidgetItem*, int)));
		disconnect(treeWidgetFunc, SIGNAL(itemDoubleClicked (QTreeWidgetItem*, int)), this, SLOT(onNavagationCheck(QTreeWidgetItem*, int)));
	}
}

